<template>
  <div class="view_page userList">
    <el-card class="box-card">
      <!--卡片头部-->
      <div slot="header" class="clearfix">
        <!--左侧-->
        <div class="left">
          <div class="menu-title">
            版本管理
          </div>
        </div>
        <!--右侧-->
        <div class="right">
          <el-button type="primary" size="small" @click="navtoAdd()"
            >新增
          </el-button>
        </div>
      </div>
      <!-- 卡片内容 -->
      <div>
        <!--筛选-->
        <div class="query-operate">
          <el-form
            class="demo-form-inline indoor-scene-buzhou-list-form"
            :inline="true"
            :model="TESTFROMDATA"
            @submit.native.prevent
          >
            <el-form-item
              label="版本号"
              label-width="120px"
              style="margin-top: 5px; margin-bottom: 5px;"
            >
              <el-input
                clearable
                @clear="searchList"
                placeholder="请输入版本号"
                v-model="TESTFROMDATA.version_num"
                @keyup.native.enter="searchList()"
              ></el-input>
            </el-form-item>
            <el-form-item
              style="margin-top: 5px; margin-bottom: 5px;margin-left: 10px;"
            >
              <el-button type="primary" @click="searchList()">查询</el-button>
            </el-form-item>
          </el-form>
        </div>

        <el-table :data="datalist" style="width: 100%">
          <el-table-column prop="id" label="ID" align="center" min-width="100">
          </el-table-column>

          <el-table-column
            prop="version_num"
            label="版本号"
            align="center"
            min-width="200"
          >
          </el-table-column>
          <el-table-column
            prop="has_enable"
            label="状态"
            align="center"
            min-width="120"
          >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.has_enable"
                @change="changestatus(scope.row)"
                inactive-color="#ff4949"
              >
              </el-switch>
            </template>
          </el-table-column>
        </el-table>
        <!--分页-->
        <pagination
          :total="total"
          :currentPage="pageNum"
          :pageSize="size"
          position="right"
          @search="searchPagination"
        >
        </pagination>
      </div>
    </el-card>

    <!--新增弹框-->
    <el-dialog
      :close-on-click-modal="false"
      title="新增版本"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form ref="form" label-width="120px">
        <el-form-item label="版本号">
          <el-input
            v-model="liveData.version_num"
            placeholder="请输入版本号"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否开启">
          <el-switch v-model="liveData.has_enable" inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import qs from "qs";
import { Message } from "element-ui";
import pagination from "@/components/common/pagination/pagination";
import devConfig from "*/dev.config";

export default {
  components: {
    pagination
  },
  watch: {},
  data() {
    return {
      // 筛选内容
      TESTFROMDATA: {
        version_num: ""
      },

      dialogVisible: false, //添加模型文件名称
      //富文本文档类型
      liveData: {
        version_num: "",
        has_enable: false
      },

      // 列表的数据
      datalist: [],
      pageNum: 1,
      page: 1,
      size: 10,
      total: 0
    };
  },
  created() {
    this.page = localStorage.getItem("page");
    // console.log(this.page)
    this.getListPage();
  },
  mounted() {},
  methods: {
    //查询列表
    getListPage() {
      var _this = this;
      var datas = {
        version_num: _this.TESTFROMDATA.version_num,
        page: _this.page,
        size: _this.size
      };
      _this.$apis.u_version_list(datas).then(res => {
        if (res.result == 0) {
          _this.datalist = res.data;
          _this.total = res.rows;
          _this.pageNum = res.page;
          localStorage.setItem("page", 1);
        }
      });
    },
    //切换状态
    changestatus(item) {
      var _this = this;
      // console.log("item:", item);
      var datas = {
        id: item.id,
        version_num: item.version_num,
        has_enable: item.has_enable
      };
      // console.log("datas:", datas);
      _this.$apis.u_version_update(datas).then(res => {
        if (res.result == 0) {
          _this.getListPage();
        }
      });
    },

    //模糊查询
    searchList() {
      this.page = 1;
      this.datalist = [];
      this.getListPage();
    },
    //分页
    searchPagination(page) {
      this.page = page;
      localStorage.setItem("page", page);
      this.getListPage();
    },

    initData() {
      this.liveData = {
        version_num: "",
        has_enable: false
      };
    },
    //跳转新增
    navtoAdd() {
      var _this = this;
      _this.initData();
      _this.dialogVisible = true;
    },
    save() {
      var _this = this;
      var datas = _this.liveData;
      if (datas.version_num == "") {
        Message.warning("版本号不能为空");
        return;
      }
      // console.log("弹窗===", datas);
      _this.$apis.u_version_add(datas).then(res => {
        if (res.result == 0) {
          Message.success("新增成功");
          this.dialogVisible = false;
          this.getListPage();
        } else {
          this.dialogVisible = false;
          this.getListPage();
        }
      });
    },
    //取消
    cancel() {
      this.dialogVisible = false;
    },
    // 去详细二级列表
    toModelFilePronList(item) {
      var _this = this;
      localStorage.setItem("page", this.page);
      var datas = {
        id: item
      };
      // _this.$apis.panorama_unity_batch_query(datas).then(res => {
      //   if (res.result == 0) {
      //     _this.$router.push({
      //       path: "/admin/modelFileManage/modelFilePronList",
      //       query: {
      //         type: type,
      //         data: JSON.stringify(res.data)
      //       }
      //     });
      //   }
      // });
    },
    //删除
    del(item) {
      var _this = this;
      var datas = {
        id: item.id
      };

      _this
        .$confirm("确定要删除【" + item.title + "】吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then(() => {
          // _this.$apis.panorama_unity_del_new(datas).then(res => {
          //   if (res.result == 0) {
          //     Message.success("删除成功");
          //     if (this.page > 1 && this.datalist.length <= 1) {
          //       this.page = this.page - 1;
          //     }
          //     _this.getListPage();
          //   }
          // });
        })
        .catch(() => {});
    }
  }
};
</script>

<style>
.el-table .cell {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: normal !important;
}
</style>
